******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script generates regression coefficients and tables comparing effect of using IV vs OLS
* Requires estout, estwide, ftools, ivreg2, reghdfe, and ivreghdfe packages

* 1st Imports Data
* 2nd Runs baseline regressions
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\Dropbox (GaTech)\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"




*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main data
*-------------------------------------------------------------------------------
* Import main dataset (source Burke et al. 2015) - contains temperature and growth variables
use data/input/GrowthClimateDataset, clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
xtset iso_id year

* Make precip in micrometers
replace UDel_precip_popweight = UDel_precip_popweight/1000
replace UDel_precip_popweight_2 = UDel_precip_popweight_2/1000^2


* Generate new economic variables
qui gen poor = (GDPpctile_WDIppp<50)
qui replace poor=. if GDPpctile_WDIppp==.
qui gen log_gdpCAP_wdi = log(gdpCAP_wdi)
qui gen loggdpCAP_wdi_l = l.log_gdpCAP_wdi

* Generate new weather variables
qui gen UDel_temp_popweight_2 = UDel_temp_popweight^2
gen poor_temp = poor*UDel_temp_popweight
gen poor_prec = poor*UDel_precip_popweight
gen poor_temp2 = poor*UDel_temp_popweight_2
gen poor_prec2 = poor*UDel_precip_popweight_2
gen dtemp = UDel_temp_popweight-l.UDel_temp_popweight
gen dprec = UDel_precip_popweight-l.UDel_precip_popweight
gen dtemp2 = UDel_temp_popweight_2-l.UDel_temp_popweight_2
gen dprec2 = UDel_precip_popweight_2-l.UDel_precip_popweight_2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2

* Generate new year variables
gen timegr = time/(1+time)
gen timegr2 = 1/2*time^2/(1+time)
*gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------
* Rename variables
ren UDel_temp_popweight temp
ren UDel_temp_popweight_2 temp2
ren UDel_precip_popweight prec
ren UDel_precip_popweight_2 prec2

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Role of IV vs OLS
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
* Step 1 - 1 lag with trend
*-------------------------------------------------------------------------------
*** Column 1 ***
eststo: reghdfe growthWDI l(0/1).(temp temp2 prec prec2), absorb(i.year  i.iso_id i.iso_id#c.year i.iso_id#c.year2) vce(cluster iso_id)
estadd local TimeTrend "Quadratic", replace
estadd local Marginal5=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2,0.0001) , replace
estadd local Marginal25=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2,0.0001) , replace

lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2
lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2


*-------------------------------------------------------------------------------
* Step 2 - 1 lag with trend and lagged growth
*-------------------------------------------------------------------------------
*** Column 2 ***
eststo: reghdfe growthWDI l(0/1).(temp temp2 prec prec2) l.growthWDI, absorb(i.year  i.iso_id i.iso_id#c.year) vce(cluster iso_id)
estadd local TimeTrend "Linear", replace
estadd local Marginal5=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2,0.0001) , replace
estadd local Marginal25=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2,0.0001) , replace

lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2
lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2

*-------------------------------------------------------------------------------
* Step 3 - 1 lag with trend and lagged growth ived
*-------------------------------------------------------------------------------
*** Column 3 ***
eststo: ivreghdfe growthWDI l(0/1).(temp temp2 prec prec2) (l.growthWDI = l3.growthWDI), absorb(i.year  i.iso_id i.iso_id#c.year) cluster(iso_id)
estadd local TimeTrend "Linear", replace
estadd local Marginal5=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2,0.0001) , replace
estadd local Marginal25=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2,0.0001) , replace

lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2
lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2

*-------------------------------------------------------------------------------
* Step 4 - no trend and lagged growth ived
*-------------------------------------------------------------------------------
*** Column 3 ***
eststo: ivreghdfe growthWDI l(0/1).(temp temp2 prec prec2) (l.growthWDI = l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local Marginal5=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2,0.0001) , replace
estadd local Marginal25=round((_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2,0.0001) , replace


lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*5*2
lincom (_b[l0.temp]+_b[l1.temp]) + (_b[l0.temp2]+_b[l1.temp2])*25*2


*-------------------------------------------------------------------------------
* Step 3 - Export Regression Coefficients
*-------------------------------------------------------------------------------
* Export regression results to LaTex Table
estwide using "tablesandfigures/RegressionCoeff_roleofiv.tex", se drop(_cons) s(TimeTrend N Marginal5 Marginal25, label("Country-Specific Time Trend" "Obs." "Marginal Effect at 5" "Marginal Effect at 25")) label addnote("All models include country and year fixed effects. Standard Errors are clustered at the country level. Temperature is measured in C. Precipitation is measured in $\mu$m/year") replace

* Clear results
eststo clear






